import com.lbl.shiro.realm.CustomRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;
import org.junit.Test;
public class CustomRealmTest {
    @Test
    public void showPwd(){//初步显示md5加密后的字符串
/*        Md5Hash md5Hash = new Md5Hash("123456");
        System.out.println(md5Hash.toString());*/
        Md5Hash md5Hash = new Md5Hash("123456","admin");
        System.out.println(md5Hash.toString());
    }
    @Test
    public void testCostomRealm(){
        CustomRealm customRealm = new CustomRealm();
        //1.构建管理器环境
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        //将用户加入进去
        securityManager.setRealm(customRealm);
        //直接md5加密
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        hashedCredentialsMatcher.setHashIterations(1);
        hashedCredentialsMatcher.setHashAlgorithmName("md5");

        customRealm.setCredentialsMatcher(hashedCredentialsMatcher);

        //2.提交认证请求
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        //认证
        UsernamePasswordToken token = new UsernamePasswordToken("admin","123456");
        subject.login(token);
        System.out.println("isAuthenticated:"+subject.isAuthenticated());
        System.out.println("checkeRoles:"+subject.hasRole("admin"));
        System.out.println("hasPermission:"+subject.isPermitted("user:delete"));
    }
}
